/**
 *
 */
package cn.hg.jfsa.manage.controller;

import java.util.List;
import java.util.Map;

import com.jfinal.kit.Kv;

import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.base.kit.UploadFileKit;

/**
 * @author golly
 */
public class UploadController extends BaseController {

	public void ajaxUpload () {
		List<Map<String, String>> list = UploadFileKit.uploadFile(this.get("path", ""), this);
		Kv kv = Kv.by("count", list.size());

		for (Map<String, String> uploaded : list) {
			String key = uploaded.keySet().iterator().next();
			kv.set(key, uploaded.get(key));
		}

		this.renderAjaxSuccessed(kv);
	}

	public void ckUpload () {
		List<Map<String, String>> list;

		try {
			list = UploadFileKit.uploadFile(this.get("path", ""), this);
		}
		catch (Exception e) {
			this.renderText("{\"uploaded\":0,\"error\":{\"message\":\"" + e.getMessage() + "\"  }}");
			return;
		}

		Kv kv = Kv.by("uploaded", 1);

		for (Map<String, String> uploaded : list) {
			String key = uploaded.keySet().iterator().next();
			kv.set("url", uploaded.get(key));
		}

		this.renderJson(kv);
	}

	@Override
	public void index () {}

}
